package cn.lyvust.first.api;


import cn.lyvust.first.manage.dto.param.evaluation.ItemAttrParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.lyvust.first.common.annotation.SysLogs;
import cn.lyvust.first.common.bean.ResponseCode;
import cn.lyvust.first.common.bean.ResponseResult;
import cn.lyvust.first.common.exception.RequestException;
import cn.lyvust.first.common.util.BeansUtil;
import cn.lyvust.first.manage.entity.EvaluationItemAttr;
import cn.lyvust.first.manage.service.EvaluationItemAttrService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author ggzheng
 * @since 2021-01-22
 */
@Api(tags = {"考核表单项"})
@RestController
@RequestMapping("/evaluation/item_attr")
public class EvaluationItemAttrController {
    @Autowired
    private EvaluationItemAttrService itemAttrService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询考核表单项")
    public ResponseResult list(@RequestBody ItemAttrParam param){
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }
        if (!StringUtils.isEmpty(param.getItemId())) {
            wrapper.eq("item_id", param.getItemId());
        }
        if (!StringUtils.isEmpty(param.getCreateBy())) {
            wrapper.eq("create_by", param.getCreateBy());
        }
        if (!StringUtils.isEmpty(param.getField())) {
            wrapper.eq("field", param.getField());
        }
        if (!StringUtils.isEmpty(param.getIsRequired())) {
            wrapper.eq("is_required", param.getIsRequired());
        }
        if (!StringUtils.isEmpty(param.getType())) {
            wrapper.eq("type", param.getType());
        }
        wrapper.orderByDesc("update_time");
        return ResponseResult.e(ResponseCode.OK, itemAttrService.list(wrapper));
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增一个考核表单项")
    @SysLogs("新增一个考核表单项")
    public ResponseResult add(@RequestBody ItemAttrParam param){
        if (StringUtils.isEmpty(param.getItemId())) {
            throw RequestException.fail("添加失败,考核表单项的指标ID不能为空!");
        }
        if (StringUtils.isEmpty(param.getField()) || StringUtils.isEmpty(param.getLabelName())) {
            throw RequestException.fail("添加失败,考核表单项名称不能为空!");
        }
        if (StringUtils.isEmpty(param.getCreateBy())) {
            throw RequestException.fail("添加失败,考核表单项添加操作人姓名不能为空!");
        }
        EvaluationItemAttr entity = new EvaluationItemAttr();
        BeanUtils.copyProperties(param, entity);
        return ResponseResult.e(ResponseCode.OK, itemAttrService.saveOrUpdate(entity));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改一个考核表单项")
    @SysLogs("修改一个考核表单项")
    public ResponseResult update(@RequestBody ItemAttrParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,所属项目主键ID不能为空!");
        }
        if (StringUtils.isEmpty(param.getCreateBy())) {
            throw RequestException.fail("修改失败,修改操作人姓名不能为空!");
        }

        EvaluationItemAttr entity = itemAttrService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, itemAttrService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除一个考核表单项")
    @SysLogs("删除一个考核表单项")
    public ResponseResult delete(@RequestBody ItemAttrParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,所属项目主键ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, itemAttrService.removeById(param.getId()));
    }
}
